package com.github.jinatonic.confetti.sample.slice;

import com.github.jinatonic.confetti.ConfettiManager;
import com.github.jinatonic.confetti.ConfettiSource;
import com.github.jinatonic.confetti.ConfettoGenerator;
import com.github.jinatonic.confetti.Utils;
import com.github.jinatonic.confetti.confetto.Confetto;
import com.github.jinatonic.confetti.confetto.ShimmeringConfetto;
import ohos.aafwk.content.Intent;
import ohos.agp.components.AttrHelper;
import ohos.agp.utils.Color;
import ohos.media.image.PixelMap;

import java.util.List;
import java.util.Random;

public class ShimmeringAbilitySlice extends AbstractAbilitySlice implements ConfettoGenerator {

    private int size;
    private int velocitySlow, velocityNormal;
    private List<PixelMap> confettoBitmaps;

    @Override
    protected void onStart(Intent savedInstanceState) {
        super.onStart(savedInstanceState);


        size = AttrHelper.vp2px(6,getContext());
        velocitySlow = AttrHelper.vp2px(50,getContext());
        velocityNormal = AttrHelper.vp2px(100,getContext());

        // The color here doesn't matter, it's simply needed to generate the bitmaps
        final int[] colors = { Color.BLACK.getValue() };
        confettoBitmaps = Utils.generateConfettiBitmaps(colors, size);
    }

    @Override
    protected ConfettiManager generateOnce() {
        return getConfettiManager().setNumInitialCount(100)
                .setEmissionDuration(0)
                .animate();
    }

    @Override
    protected ConfettiManager generateStream() {
        return getConfettiManager().setNumInitialCount(0)
                .setEmissionDuration(3000)
                .setEmissionRate(50)
                .animate();
    }

    @Override
    protected ConfettiManager generateInfinite() {
        return getConfettiManager().setNumInitialCount(0)
                .setEmissionDuration(ConfettiManager.INFINITE_DURATION)
                .setEmissionRate(50)
                .animate();
    }

    private ConfettiManager getConfettiManager() {
        final ConfettiSource confettiSource = new ConfettiSource(0, -size, container.getWidth(),
                -size);
        return new ConfettiManager(this, this, confettiSource, container)
                .setVelocityX(0, velocitySlow)
                .setVelocityY(velocityNormal, velocitySlow)
                .setInitialRotation(180, 180)
                .setRotationalAcceleration(360, 180)
                .setTargetRotationalVelocity(360);
    }

    @Override
    public Confetto generateConfetto(Random random) {
        return new ShimmeringConfetto(
                confettoBitmaps.get(random.nextInt(confettoBitmaps.size())),
                getGoldLight(), getGoldDark(), 1000, random);
    }
}
